package com.work.controller;

import com.google.gson.JsonObject;
import com.work.entity.Weather;
import com.work.entity.WeatherDay;
import com.work.entity.WeatherHour;
import com.work.service.WeatherService;
import com.work.service.impl.WeatherServiceImpl;
import com.work.utils.JsonUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by baichenguang on 2020/2/18.
 */
@Controller
@RequestMapping("/weather")
public class WertherController {
    private WeatherService weatherDao = new WeatherServiceImpl();

    /**
     * 分页查询 原始数据
     * @param pageSize
     * @param curPage
     * @return
     */
    @RequestMapping("weather.json")
    @ResponseBody
    public Object getData(String pageSize,String curPage){
        if (pageSize == null || pageSize.equals("") || curPage == null || curPage.equals("")) {
            return null;
        }
        List<Weather> weathers = null;
        try{
            int size = Integer.parseInt(pageSize);
            int cur = Integer.parseInt(curPage);
            weathers = weatherDao.getWeatherList(size,cur);
            Object obj = JsonUtil.buildJson(weathers);
            return obj;
        }catch (Exception e){
            e.printStackTrace();
            return JsonUtil.objectToJson(e.getMessage());
        }
    }
    /**
     * 获取时间段内的数据
     * @param start unix时间戳 毫秒级别
     * @param end
     * @return
     */
    @RequestMapping("weatherByTime.json")
    @ResponseBody
    public Object getDataByTime(String start, String end) {
        List<Weather> weathers = null;
        try {
            if (start == null || start.equals("") || end == null || end.equals("")) {
                return null;
            }
            long start_long = Long.parseLong(start);
            long end_long = Long.parseLong(end);
            weathers = weatherDao.getWeatherListByDate(new Date(start_long), new Date(end_long));
            Object obj = JsonUtil.buildJson(weathers);
            return obj;
        } catch (Exception e) {
            e.printStackTrace();
            return JsonUtil.objectToJson(e.getMessage());
        }
    }

    /**
     * 获取按月份的数据 每天只要一条
     * @param year
     * @param month
     * @return
     */
    @RequestMapping("weatherByMonth.json")
    @ResponseBody
    public Object getDataByMonth(String year,String month){
        List<WeatherDay> weathers = null;
        try{
            if (year == null || year.equals("") || month == null || month.equals("")) {
                return null;
            }
            int y = Integer.parseInt(year);
            int m = Integer.parseInt(month);
            weathers = weatherDao.getWeatherListByMonth(y,m);
            Object obj = JsonUtil.buildJson(weathers);
            return obj;
        }catch (Exception e){
            e.printStackTrace();
            return JsonUtil.objectToJson(e.getMessage());
        }
    }

    /**
     * 获取按小时的数据 每小时只有一条
     * @param year
     * @param month
     * @param day
     * @return
     */
    @RequestMapping("weatherByDay.json")
    @ResponseBody
    public Object getDataByDay(String year,String month,String day){
        List<WeatherHour> weathers = null;
        try{
            if (year == null || year.equals("") || month == null || month.equals("")||day == null || day.equals("")) {
                return null;
            }
            int y = Integer.parseInt(year);
            int m = Integer.parseInt(month);
            int d = Integer.parseInt(day);
            weathers = weatherDao.getWeatherListByDay(y,m,d);
            Object obj = JsonUtil.buildJson(weathers);
            return obj;
        }catch (Exception e){
            e.printStackTrace();
            return JsonUtil.objectToJson(e.getMessage());
        }
    }
}
